<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="自然村" prop="natureVillages">
        <el-input v-model="queryParams.natureVillages" placeholder="请输入自然村" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="户籍编号" prop="homeId">
        <el-input v-model="queryParams.homeId" placeholder="请输入户籍编号" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="人编号" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入人编号" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="证件号码" prop="idcard">
        <el-input v-model="queryParams.idcard" placeholder="请输入证件号码" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="人均纯收入" prop="income">
        <el-input v-model="queryParams.income" placeholder="请输入人均纯收入" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="cellPhone">
        <el-input v-model="queryParams.cellPhone" placeholder="请输入联系电话" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="计划开工时间">
        <el-date-picker v-model="dateRangePlannedStartTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="计划竣工时间">
        <el-date-picker v-model="dateRangePlannedCompletionTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['pams:userinfo:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['pams:userinfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="县（市、区、旗）" align="center" prop="address" />
      <el-table-column label="乡镇" align="center" prop="township" />
      <el-table-column label="行政村" align="center" prop="villages" />
      <el-table-column label="自然村" align="center" prop="natureVillages" />
      <el-table-column label="户籍编号" align="center" prop="homeId" />
      <el-table-column label="人编号" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="证件号码" align="center" prop="idcard" />
      <el-table-column label="人数" align="center" prop="userNumber" />
      <el-table-column label="与户主关系" align="center" prop="relationship" />
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="文化程度" align="center" prop="educationLevel" />
      <el-table-column label="在校生状况" align="center" prop="studentStatus" />
      <el-table-column label="健康状况" align="center" prop="healthStatus" />
      <el-table-column label="劳动技能" align="center" prop="laborSkills" />
      <el-table-column label="务工情况" align="center" prop="employmentSituation" />
      <el-table-column label="务工时间（月）" align="center" prop="employmentTime" />
      <el-table-column label="参加大病医疗" align="center" prop="medical" />
      <el-table-column label="脱贫属性" align="center" prop="poverty" />
      <el-table-column label="脱贫年度" align="center" prop="povertyYear" />
      <el-table-column label="致贫原因" align="center" prop="povertyReason" />
      <el-table-column label="危房户" align="center" prop="dilapidatedHouse" />
      <el-table-column label="是否解决安全饮用水" align="center" prop="safeWater" />
      <el-table-column label="人均纯收入" align="center" prop="income" />
      <el-table-column label="联系电话" align="center" prop="cellPhone" />
      <el-table-column label="首次识别时间" align="center" prop="begainTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.begainTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="贫困人员识别时间" align="center" prop="identificationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.identificationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否受损" align="center" prop="damaged" />
      <el-table-column label="临时保障方式" align="center" prop="shortTerm" />
      <el-table-column label="长期保障方式" align="center" prop="longTerm" />
      <el-table-column label="计划开工时间" align="center" prop="plannedStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plannedStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划竣工时间" align="center" prop="plannedCompletionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plannedCompletionTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['pams:userinfo:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['pams:userinfo:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="县（市、区、旗）" prop="address">
          <el-input v-model="form.address" placeholder="请输入县（市、区、旗）" />
        </el-form-item>
        <el-form-item label="乡镇" prop="township">
          <el-input v-model="form.township" placeholder="请输入乡镇" />
        </el-form-item>
        <el-form-item label="行政村" prop="villages">
          <el-input v-model="form.villages" placeholder="请输入行政村" />
        </el-form-item>
        <el-form-item label="自然村" prop="natureVillages">
          <el-input v-model="form.natureVillages" placeholder="请输入自然村" />
        </el-form-item>
        <el-form-item label="户籍编号" prop="homeId">
          <el-input v-model="form.homeId" placeholder="请输入户籍编号" />
        </el-form-item>
        <el-form-item label="人编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入人编号" />
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="人数" prop="userNumber">
          <el-input v-model="form.userNumber" placeholder="请输入人数" />
        </el-form-item>
        <el-form-item label="与户主关系" prop="relationship">
          <el-input v-model="form.relationship" placeholder="请输入与户主关系" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="文化程度" prop="educationLevel">
          <el-input v-model="form.educationLevel" placeholder="请输入文化程度" />
        </el-form-item>
        <el-form-item label="在校生状况" prop="studentStatus">
          <el-radio-group v-model="form.studentStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="健康状况" prop="healthStatus">
          <el-radio-group v-model="form.healthStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="劳动技能" prop="laborSkills">
          <el-input v-model="form.laborSkills" placeholder="请输入劳动技能" />
        </el-form-item>
        <el-form-item label="务工情况" prop="employmentSituation">
          <el-input v-model="form.employmentSituation" placeholder="请输入务工情况" />
        </el-form-item>
        <el-form-item label="务工时间（月）" prop="employmentTime">
          <el-date-picker clearable size="small" v-model="form.employmentTime" type="date" value-format="yyyy-MM-dd" placeholder="选择务工时间（月）" />
        </el-form-item>
        <el-form-item label="参加大病医疗" prop="medical">
          <el-radio-group v-model="form.medical">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="脱贫属性" prop="poverty">
          <el-radio-group v-model="form.poverty">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="脱贫年度" prop="povertyYear">
          <el-input v-model="form.povertyYear" placeholder="请输入脱贫年度" />
        </el-form-item>
        <el-form-item label="致贫原因" prop="povertyReason">
          <el-input v-model="form.povertyReason" placeholder="请输入致贫原因" />
        </el-form-item>
        <el-form-item label="危房户" prop="dilapidatedHouse">
          <el-radio-group v-model="form.dilapidatedHouse">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否解决安全饮用水" prop="safeWater">
          <el-radio-group v-model="form.safeWater">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="人均纯收入" prop="income">
          <el-input v-model="form.income" placeholder="请输入人均纯收入" />
        </el-form-item>
        <el-form-item label="联系电话" prop="cellPhone">
          <el-input v-model="form.cellPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="首次识别时间" prop="begainTime">
          <el-date-picker clearable size="small" v-model="form.begainTime" type="date" value-format="yyyy-MM-dd" placeholder="选择首次识别时间" />
        </el-form-item>
        <el-form-item label="贫困人员识别时间" prop="identificationTime">
          <el-date-picker clearable size="small" v-model="form.identificationTime" type="date" value-format="yyyy-MM-dd" placeholder="选择贫困人员识别时间" />
        </el-form-item>
        <el-form-item label="是否受损" prop="damaged">
          <el-radio-group v-model="form.damaged">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="临时保障方式" prop="shortTerm">
          <el-input v-model="form.shortTerm" placeholder="请输入临时保障方式" />
        </el-form-item>
        <el-form-item label="长期保障方式" prop="longTerm">
          <el-input v-model="form.longTerm" placeholder="请输入长期保障方式" />
        </el-form-item>
        <el-form-item label="计划开工时间" prop="plannedStartTime">
          <el-date-picker clearable size="small" v-model="form.plannedStartTime" type="date" value-format="yyyy-MM-dd" placeholder="选择计划开工时间" />
        </el-form-item>
        <el-form-item label="计划竣工时间" prop="plannedCompletionTime">
          <el-date-picker clearable size="small" v-model="form.plannedCompletionTime" type="date" value-format="yyyy-MM-dd" placeholder="选择计划竣工时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createUserinfo, updateUserinfo, deleteUserinfo, getUserinfo, getUserinfoPage, exportUserinfoExcel } from "@/api/pams/userinfo";

export default {
  name: "Userinfo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 贫困档案列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangePlannedStartTime: [],
      dateRangePlannedCompletionTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        natureVillages: null,
        homeId: null,
        userId: null,
        userName: null,
        idcard: null,
        income: null,
        cellPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        address: [{ required: true, message: "县（市、区、旗）不能为空", trigger: "blur" }],
        township: [{ required: true, message: "乡镇不能为空", trigger: "blur" }],
        villages: [{ required: true, message: "行政村不能为空", trigger: "blur" }],
        natureVillages: [{ required: true, message: "自然村不能为空", trigger: "blur" }],
        homeId: [{ required: true, message: "户籍编号不能为空", trigger: "blur" }],
        userId: [{ required: true, message: "人编号不能为空", trigger: "blur" }],
        userName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        idcard: [{ required: true, message: "证件号码不能为空", trigger: "blur" }],
        userNumber: [{ required: true, message: "人数不能为空", trigger: "blur" }],
        relationship: [{ required: true, message: "与户主关系不能为空", trigger: "blur" }],
        nation: [{ required: true, message: "民族不能为空", trigger: "blur" }],
        healthStatus: [{ required: true, message: "健康状况不能为空", trigger: "blur" }],
        laborSkills: [{ required: true, message: "劳动技能不能为空", trigger: "blur" }],
        medical: [{ required: true, message: "参加大病医疗不能为空", trigger: "blur" }],
        poverty: [{ required: true, message: "脱贫属性不能为空", trigger: "blur" }],
        povertyYear: [{ required: true, message: "脱贫年度不能为空", trigger: "blur" }],
        povertyReason: [{ required: true, message: "致贫原因不能为空", trigger: "blur" }],
        dilapidatedHouse: [{ required: true, message: "危房户不能为空", trigger: "blur" }],
        safeWater: [{ required: true, message: "是否解决安全饮用水不能为空", trigger: "blur" }],
        income: [{ required: true, message: "人均纯收入不能为空", trigger: "blur" }],
        cellPhone: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangePlannedStartTime, 'plannedStartTime');
      this.addBeginAndEndTime(params, this.dateRangePlannedCompletionTime, 'plannedCompletionTime');
      // 执行查询
      getUserinfoPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        address: undefined,
        township: undefined,
        villages: undefined,
        natureVillages: undefined,
        homeId: undefined,
        userId: undefined,
        userName: undefined,
        idcard: undefined,
        userNumber: undefined,
        relationship: undefined,
        nation: undefined,
        educationLevel: undefined,
        studentStatus: undefined,
        healthStatus: undefined,
        laborSkills: undefined,
        employmentSituation: undefined,
        employmentTime: undefined,
        medical: undefined,
        poverty: undefined,
        povertyYear: undefined,
        povertyReason: undefined,
        dilapidatedHouse: undefined,
        safeWater: undefined,
        income: undefined,
        cellPhone: undefined,
        begainTime: undefined,
        identificationTime: undefined,
        damaged: undefined,
        shortTerm: undefined,
        longTerm: undefined,
        plannedStartTime: undefined,
        plannedCompletionTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangePlannedStartTime = [];
      this.dateRangePlannedCompletionTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加贫困档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getUserinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改贫困档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateUserinfo(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createUserinfo(this.form).then(response => {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除贫困档案编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteUserinfo(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangePlannedStartTime, 'plannedStartTime');
      this.addBeginAndEndTime(params, this.dateRangePlannedCompletionTime, 'plannedCompletionTime');
      // 执行导出
      this.$confirm('是否确认导出所有贫困档案数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserinfoExcel(params);
        }).then(response => {
          this.downloadExcel(response, '贫困档案.xls');
        })
    }
  }
};
</script>
